﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class Member : MemberBase
    {
        #region Constructor
        public Member() 
        {
            this.Id = -1;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.Email = string.Empty;
            this.Locked = false;
            this.ActiveCode = string.Empty;
            this.Fullname = string.Empty;
            this.Address = string.Empty;
            this.Mobile = string.Empty;
        }
        #endregion

        #region List
        public DataTable GetAll(string SortColumn)
        {
            string strQuery = string.Empty;
            if(SortColumn=="")
                strQuery = "select * from Member";
            else
                strQuery = "select * from Member order by " + SortColumn;
            return ExecuteDataset(false, strQuery).Tables[0];
        }       

        public bool Delete(string id)
        {
            string strQuery = "Delete from Member where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            return ExecuteNonQuery(false, strQuery, Id) > 0;
        }
        #endregion

        #region Edit
        public void GetSingle()
        {
            if (Exist())
            {
                string strQuery = "select * from Member, MemberProfile where Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);                
                DataTable dt = ExecuteDataset(false, strQuery, Id).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Username = dt.Rows[0]["Username"].ToString();
                    this.Email = dt.Rows[0]["Email"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Locked"].ToString()))
                        this.Locked = (bool)dt.Rows[0]["Locked"];
                    this.ActiveCode = dt.Rows[0]["ActiveCode"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["CreateDate"].ToString()))
                        this.CreateDate = (DateTime)dt.Rows[0]["CreateDate"];
                    this.Fullname = dt.Rows[0]["Fullname"].ToString();
                    this.Address = dt.Rows[0]["Address"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Birthday"].ToString()))
                        this.Birthday = (DateTime)dt.Rows[0]["Birthday"];                    
                    this.Mobile = dt.Rows[0]["Mobile"].ToString();
                }
            }
            else this.Id = -1;
        }

        public bool Exist()
        {
            string strQuery = "select Id from Member where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(false, strQuery, Id) != null;
        }

        public bool Active(string id)
        {
            string strQuery = "update Member set ActiveCode=null where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);

            return ExecuteNonQuery(false, strQuery, Id) > 0;
        }

        public bool Lock(string id, bool status)
        {
            string strQuery = "update Member set Locked=@Status where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter Status = new SqlParameter("@Status", status);

            return ExecuteNonQuery(false, strQuery, Id, Status) > 0;
        }
        #endregion       
    }
}
